package com.quiz.quazz;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.quiz.quazz.client.Player;
import com.quiz.quazz.client.TcpClient;
import com.quiz.quazz.server.Server;
import com.quiz.quazz.util.ConnectTask;
import com.quiz.quazz.util.IpConfigUtil;

public class LobbyActivity extends Activity {
	private TextView textViewGameName;

	private TextView textViewPlayerCounterNumber;

	private TcpClient mTcpClient;

	private ListView gameListView;

	private boolean isServer;

	private Server server;

	private String serverIp;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lobby);

		textViewPlayerCounterNumber = (TextView) findViewById(R.id.textViewPlayerCountNumber);
		textViewGameName = (TextView) findViewById(R.id.textViewGameName);
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				textViewGameName.setText(IpConfigUtil.getIPAddress(true));
			}
		});

		gameListView = (ListView) findViewById(R.id.listViewGames);

		Button buttonStart = (Button) findViewById(R.id.button_start_game);

		buttonStart.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (isServer) {
					server.sendMessageToAllClients("message to all clients");
				} else {
					mTcpClient.sendMessage("message from client to Server");
				}
			}
		});
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		this.serverIp = getIntent().getStringExtra("serverIp");
		this.isServer = getIntent().getBooleanExtra("isServer", false);
		int serverPort = getIntent().getIntExtra("serverPort", 0);
		String playerName = getIntent().getStringExtra("playerName");
		if (isServer) {
			server = new Server();
			server.setMessageListener(new Server.OnMessageReceived() {
				@Override
				public void messageReceived(String message) {
					System.out.println("server " + IpConfigUtil.getIPAddress(true) + ": " + message);
				}
			});
			server.startServer(serverPort);
			ConnectTask connectTask = new ConnectTask(serverIp, serverPort, playerName);
			connectTask.setOnMessageReceivedListener(new TcpClient.OnMessageReceived() {
				@Override
				public void messageReceived(String message) {
					System.out.println("client " + IpConfigUtil.getIPAddress(true) + ": " + message);
				}
			});
			connectTask.execute("");
		} else {
			ConnectTask connectTask = new ConnectTask(serverIp, serverPort, playerName);
			connectTask.setOnMessageReceivedListener(new TcpClient.OnMessageReceived() {
				@Override
				public void messageReceived(String message) {
					System.out.println("client " + IpConfigUtil.getIPAddress(true) + ": " + message);
				}
			});
			connectTask.execute("");
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					textViewGameName.setText(serverIp);
				}
			});
		}
		mTcpClient = TcpClient.getInstance();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (server != null)
			server.stopServer();

		if (mTcpClient != null) {
			mTcpClient.stopClient();
			mTcpClient = null;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (server != null)
			server.stopServer();

		if (mTcpClient != null) {
			mTcpClient.stopClient();
			mTcpClient = null;
		}
	}

	private void update() {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				// final PlayerArrayAdapter adapter =
				// new PlayerArrayAdapter(LobbyActivity.this, android.R.layout.simple_list_item_1, serverRunnable
				// .getPlayerList().getPlayers());
				// gameListView.setAdapter(adapter);
				//
				// textViewPlayerCounterNumber.setText(serverRunnable.getPlayerList().getPlayerCount().toString());
			}
		});
	}

	private class PlayerArrayAdapter extends ArrayAdapter<Player> {

		HashMap<Player, Integer> playerMap = new HashMap<Player, Integer>();

		public PlayerArrayAdapter(Context context, int textViewResourceId, List<Player> players) {
			super(context, textViewResourceId, players);
			for (int i = 0; i < players.size(); ++i) {
				playerMap.put(players.get(i), i);
			}
		}

		@Override
		public long getItemId(int position) {
			Player item = getItem(position);
			return playerMap.get(item);
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

	}

}